package com.saprk.demoarithmetic.leetcode;

import java.util.HashMap;
import java.util.Optional;

/**
 * @author fuqiang
 * @Description 简单题目
 * @Date 2021-05-14
 */
public class Easy {


    //===================================两数之和======================================

    /**
     * 暴力循环 两数之和
     * @author fuqiang
     * @date 2021-05-14 15:56
     * @param nums
     * @param target
     * @return int[]
     */
    public int[] twoSum(int[] nums, int target) {

        int[] result = new int[2];

        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if(i!=j && nums[i]+nums[j] == target){
                    result[0] = i;
                    result[1] = j;
                }
            }
        }
        return result;
    }
    /**
     * 使用 hashmap key value 形式获取目标值
     * @author fuqiang
     * @date 2021-05-14 16:05
     * @param nums
     * @param target
     * @return int[]
     */
    public static int[] twoSum_1(int[] nums, int target) {

        int[] result = new int[2];
        HashMap<Integer,Integer> hashMap = new HashMap<>(nums.length);

        for (int i = 0; i < nums.length; i++) {
            if(hashMap.containsKey(target - nums[i])){
                Integer integer = hashMap.get(target - nums[i]);
                return new int[]{ integer,i};
            }

            hashMap.put(nums[i],i);
        }
        return result;
    }

    // ===============================两数相加=======================================
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public static ListNode addTwoNumbers_2(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null){
            int v1 = Optional.ofNullable(l1).orElseGet(()->new ListNode(0)).val;
            int v2 = Optional.ofNullable(l2).orElseGet(()->new ListNode(0)).val;
            int sum = v1 + v2 + carry;

            if(head == null){
                head = tail = new ListNode(sum % 10);
            }else{
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;

            if(l1 != null){
                l1 = l1.next;
            }
            if(l2 != null){
                l2 = l2.next;
            }
        }
        if(1 == carry){
            tail.next = new ListNode(carry);
        }

        return head;
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        long numL1 = 0;
        long numL2 = 0;
        // 计算第一个链表数值
        ListNode tempList1 = l1;
        for (int i = 0; tempList1 != null ; i++) {
            long temp = (long) (tempList1.val * Math.pow(10,i));
            numL1 += temp;
            tempList1 = tempList1.next;
        }
        // 计算第二个链表数值
        ListNode tempList2 = l2;
        for (int i = 0; tempList2 != null ; i++) {
            long temp = (long) (tempList2.val * Math.pow(10,i));
            numL2 += temp;
            tempList2 = tempList2.next;
        }
        // 最后结果
        long numResult = numL1 + numL2; // 假如 1025

        ListNode pre = new ListNode();
        ListNode cur = pre;

        while(numResult > 0){
            cur.val = (int) (numResult % 10);
            if(numResult >= 10 ){
                cur.next = new ListNode();
                cur = cur.next;
            }
            numResult = numResult/10;
        }

        return pre;

    }
    /**
     * 链表反转
     * @author fuqiang
     * @date 2021/10/28 9:46
     * @param head
     * @return com.saprk.demoarithmetic.leetcode.Easy.ListNode
     */
    public static ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        // 获取到最后一个节点
        ListNode newListHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newListHead;
    }


    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2,node1);
        ListNode node3 = new ListNode(3,node2);
        ListNode node4 = new ListNode(4,node3);
        ListNode listNode = reverseList(node4);
        while (true){
            if (listNode == null) {
                break;
            }
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }
}

class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}